package com.xcity.game.combat.skill.impl;

import java.util.List;

import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.ActionEffect.Value;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.skill.Skill;

/**
 * 强身健体<br/>
 * 为所有队员增加护盾，护盾可以吸收{0}%生命值伤害，在护盾生效期间被保护者增加{1}%攻击力
 * @author yang.li
 *
 */
public class COMBAT_SKILL_90115 extends CombatSkill {

	public COMBAT_SKILL_90115(Skill skill) {
		super(skill);
	}

	@Override
	public void calcDamage(Action action) {
		CombatUnit source = action.unit;
		List<CombatUnit> units = source.getTeam().all();
		for (int i = 0; i < units.size(); i++) {
			CombatUnit unit = units.get(i);
			if (!unit.isDied()) {
				CombatBuff buff1 = new BUFF1(skill, unit);
				action.addBuff(buff1);
				action.addBuff(new BUFF2(skill, unit, buff1));
			}
		}
	}
	
	// 护盾
	static class BUFF1 extends CombatBuff {
		int hp;
		public BUFF1(Skill skill, CombatUnit owner) {
			super(skill.getTemplate().getCombatBuff(0), owner);
			this.hp = (int) (owner.getMaxHp() * skill.getParameter(0) / GameStaticConfig.percentBase);
		}
		@Override
		public void preDamage(ActionEffect effect) {
			int delta = effect.getHpDelta();
			if (delta < 0 && hp > 0) {
				hp += delta;
				Value v = effect.getValue(CombatAttributeType.HP);
				v.setDelta(hp > 0 ? 0 : hp);
			}
		}
		@Override
		public void afterAction(Action action) {
			if (hp <= 0 && state == STATE_ADDED) { // 破盾
				uneffect();
				action.ref().addBuff(this);
			}
		}
	}
	
	// 加攻
	static class BUFF2 extends CombatBuff {
		CombatBuff buff1; // 护盾buff
		float preAttackPower;
		public BUFF2(Skill skill, CombatUnit owner, CombatBuff buff1) {
			super(skill.getTemplate().getCombatBuff(1), owner);
			this.buff1 = buff1;
			float attackPower = owner.getAttackPower();
			preAttackPower = attackPower;
			float ratio = skill.getParameter(1) / GameStaticConfig.percentBase;
			attackPower += attackPower * ratio;
			owner.setAttributeValue(CombatAttributeType.ATTACK_POWER, attackPower);
		}
		@Override
		public void afterAction(Action action) {
			roundEnd(action.ref());
		}
		@Override
		public void roundEnd(Round round) {
			if (buff1.getState() == STATE_REMOVED  && state == STATE_ADDED) { // 破盾
				uneffect();
				round.addBuff(this);
				owner.setAttributeValue(CombatAttributeType.ATTACK_POWER, preAttackPower);
			}
		}
	}

}
